<template>
   <div class="search-div">
    <!-- 搜索表单 -->
    <el-form label-width="70px" size="small">


      <el-form-item label="文章">
          <el-select v-model="queryDto.articleId" placeholder="请选择">
            <el-option v-for="item in article" :key="item.id" :label="item.articleName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

   
      <el-form-item label="科室">
        <el-select v-model="queryDto.roomId" placeholder="请选择">
          <el-option v-for="item in division" :key="item.id" :label="item.sname" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="目标人群">
          <el-select v-model="queryDto.targetPopulationId" placeholder="请选择">
            <el-option v-for="item in Population" :key="item.id" :label="item.targetPopulationName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

   
        <el-form-item label="发送时间">
            <el-input type="date" v-model="queryDto.dispatchDate"/>
        </el-form-item>
 
      <el-row style="display:flex">
        <el-button type="primary" size="small" @click="searchSysRole">
          搜索
        </el-button>
        <el-button size="small" @click="resetData">重置</el-button>
      </el-row>
    </el-form>


<el-dialog v-model="dialogVisible" title="宣教" width="30%">
    <el-form label-width="120px">
        <el-form-item label="发送时间">
            <el-input type="date" v-model="healtheducation.dispatchDate"/>
        </el-form-item>

        <el-form-item label="文章">
          <el-select v-model="healtheducation.articleId" placeholder="请选择">
            <el-option v-for="item in article" :key="item.id" :label="item.articleName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="科室">
          <el-select v-model="healtheducation.roomId" placeholder="请选择">
            <el-option v-for="item in division" :key="item.id" :label="item.sname" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>



        <el-form-item label="发送人员">
          <el-select v-model="healtheducation.senderId" placeholder="请选择">
            <el-option v-for="item in Users" :key="item.id" :label="item.username" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="目标人群">
          <el-select v-model="healtheducation.targetPopulationId" placeholder="请选择">
            <el-option v-for="item in Population" :key="item.id" :label="item.targetPopulationName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

          <el-form-item label="宣教内容">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="healtheducation.content">
          </el-input>
          </el-form-item>



        <el-form-item>
            <el-button type="primary" @click="submit()">提交</el-button>
            <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
    </el-form>
</el-dialog>

        <!-- 添加按钮 -->
        <div class="tools-div">
            <el-button type="success" size="small" @click="addShow">发起宣教</el-button>
        </div>
        
        <!--- 角色表格数据 -->
        <el-table :data="list" style="width: 100%">
            <el-table-column prop="id" label="序号" width="170" />
            <el-table-column prop="articleName" label="文章" width="170" />
            <el-table-column prop="sname" label="科室" width="170" />
            <el-table-column prop="username" label="发送人员" width="170" />
            <el-table-column prop="content" label="宣教内容" width="170" />
            <el-table-column prop="targetPopulationName" label="目标人群" width="170" />
            <el-table-column prop="dispatchDate" label="发送日期" width="170" />
            <el-table-column label="操作" align="center" width="180" #default="scope">
                <el-button type="danger" size="small" @click="deleteById(scope.row)">
                    删除
                </el-button>
                <el-button type="primary" size="small" @click="editShow(scope.row)">
            修改
          </el-button>
            </el-table-column>
        </el-table>

        <!--分页条-->
        <el-pagination
               v-model:current-page="pageParams.page"
               v-model:page-size="pageParams.limit"
               :page-sizes="[10, 20, 50, 100]"
               @size-change="fetchData"
               @current-change="fetchData"
               layout="total, sizes, prev, pager, next"
               :total="total"
/>   
  </div>
</template>

<script setup ts>
import { ref , onMounted } from 'vue';
import { healtheducationPageList , Delhealtheducation ,Savehealtheducation  ,findPopulation ,findUsers ,Updatehealtheducation} from '@/api/yingfei/healtheducation'
import{ findDivision } from "@/api/division/division"
import { articleList } from "@/api/yingfei/article"
import { ElMessage, ElMessageBox } from 'element-plus'


// 分页条总记录数
let total = ref(0)

// 定义表格数据模型
let list = ref([])

//分页数据
const pageParamsForm = {
  page: 1, // 页码
  limit: 3, // 每页记录数
}
const pageParams = ref(pageParamsForm)     // 将pageParamsForm包装成支持响应式的对象
const division = ref({})
const article = ref({})
const Population = ref({})
const Users = ref({})
const options=ref([])

// 搜索表单数据
const queryDto = ref({
sname: "",
articleId: "",
articleName: "",
dispatchDate: ""

})

// 页面加载完毕以后请求后端接口获取数据
onMounted(() => {
    fetchData() ;
    findDivisions();
    findarticles();
    findPopulations();
    findUserss()

})

// 搜索按钮点击事件处理函数
const searchSysRole = () => {
  fetchData();
}



// 远程调用后端分页查询接口
const fetchData = async () => {
    const {data , code , message } = await healtheducationPageList(pageParams.value.page , pageParams.value.limit , queryDto.value) ;
    list.value = data.list ;
    total.value = data.total
}

// 删除数据
const deleteById = (row) => {
    ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', 'Warning', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(async () => {
       const {code } = await Delhealtheducation(row.id)
       if(code === 200) {
            ElMessage.success('删除成功')
            pageParams.value.page = 1
            fetchData()
       }
    })
}

const defaultForm = {       //页面表单数据
  id: '',
  articleId : '',
  categoryId: ''
}
const healtheducation = ref(defaultForm)    
const dialogVisible = ref(false)

//进入添加
const addShow = () => {
    healtheducation.value = {}
    dialogVisible.value = true
}

// 进入修改
const editShow = row => {
  healtheducation.value = row
  dialogVisible.value = true
}

//添加
const submit = async()=>{
    if(!healtheducation.value.id) {
        const { code } = await Savehealtheducation(healtheducation.value) ;
        if(code === 200) {
            dialogVisible.value = false
            ElMessage.success('操作成功')
            fetchData()
        }
//修改
    }else {
        const { code } = await Updatehealtheducation(healtheducation.value) ;
        if(code === 200) {
            dialogVisible.value = false
            ElMessage.success('操作成功')
            fetchData()
        }
    }
}


//查询全部科室
const findDivisions = async () => {
  const { data, code } = await findDivision();
  console.log(data.list);
  division.value = data.list
}

//
const findarticles = async () => {
  const { data, code } = await articleList();
  console.log(data.list);
  article.value = data.list
}

//查询目标人群
const findPopulations = async () => {
  const { data, code } = await findPopulation();
  console.log(data.list);
  Population.value = data.list
}

//发送人员
const findUserss = async () => {
  const { data, code } = await findUsers();
  console.log(data.list);
  Users.value = data.list
}

</script>

<style scoped>

.search-div {
  margin-bottom: 10px;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}

.tools-div {
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}

</style>